package com.example.supplychainbackend.controller;

import com.example.supplychainbackend.controller.dto.DataResponse;
import com.example.supplychainbackend.controller.dto.TodoDto;
import com.example.supplychainbackend.controller.dto.user.*;
import com.example.supplychainbackend.dao.TodoDao;
import com.example.supplychainbackend.entity.TodoEntity;
import com.example.supplychainbackend.entity.UserEntity;
import com.example.supplychainbackend.service.TodoService;
import com.example.supplychainbackend.tool.JwtUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("api/todo")
public class TodoController {
    private final TodoDao todoDao;
    private final TodoService todoService;

    @PostMapping("/handle/{id}")
    public DataResponse<Boolean> handle(@PathVariable String id) {
        return DataResponse.success(todoService.handleTodo(id));
    }

    @GetMapping("/list")
    public DataResponse<List<TodoDto>> getEnterpriseTodo(@RequestHeader("Authorization") String authHeader
                                             ) {
        if (!JwtUtil.validateToken(authHeader)) {
            return new DataResponse<>(401, "Invalid token",
                    null);
        }
        UserSigDto sig = JwtUtil.getUserSignatureFromToken(authHeader);
        return DataResponse.success(todoService.getAllTodoByEid(sig.getEnterpriseId()));
    }

    @GetMapping("/listall")
    public DataResponse<List<TodoDto>> getAllTodo() {
        return DataResponse.success(todoService.getAllTodo());
    }

}